package com.todo.pro.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.todo.pro.entity.BaiduUserFace;
import com.todo.pro.jwt.JwtUtil;
import com.todo.pro.res.JsonResult;
import com.todo.pro.res.model.BaiduUserFaceResult;
import com.todo.pro.service.BaiduUserFaceService;
import com.todo.pro.util.MapUtil;

/**
 * @author SiZhongxia
 * @since 2017-12-27
 */
@Controller
@RequestMapping("baiduUserFace")
public class BaiduUserFaceController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(BaiduUserFaceController.class);

	@Autowired
	private BaiduUserFaceService baiduUserFaceService;

	@RequiresAuthentication
	@RequestMapping(value = "list")
	public ModelAndView list(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("modules/baiduUserFace/list");
		mav.addObject("csrftoken",
				JwtUtil.createToken(MapUtil.init().putVal("path", "gq-baiduUserFace-list"), 2L * 60 * 60 * 1000));
		return mav;
	}

	@ResponseBody
	@RequiresAuthentication
	@RequestMapping(value = "data")
	public JsonResult<List<BaiduUserFaceResult>> data(HttpServletRequest request) {
		Wrapper<BaiduUserFace> baiduUserFaceWrapper = new EntityWrapper<>();
		baiduUserFaceWrapper.orderBy("create_time", false);
		List<BaiduUserFace> baiduUserFaces = baiduUserFaceService.selectList(baiduUserFaceWrapper);

		List<BaiduUserFaceResult> baiduUserFacers = new ArrayList<>();
		int count = baiduUserFaces.size();
		if (count > 0) {
			for (BaiduUserFace baiduUserFace : baiduUserFaces) {
				BaiduUserFaceResult baiduUserFacer = new BaiduUserFaceResult();
				BeanUtils.copyProperties(baiduUserFace, baiduUserFacer);
				baiduUserFacer.setId(baiduUserFace.getId().toString());
				baiduUserFacer.setUserId(baiduUserFace.getUserId().toString());
				baiduUserFacers.add(baiduUserFacer);
			}
		}
		return new JsonResult<List<BaiduUserFaceResult>>(count, baiduUserFacers);
	}

}
